﻿using BTL_Cloud.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TL_Cloud.Core.Entities;
using TL_Cloud.Core.Services;
using TL_Cloud.DbRepositoryInterface.BookShop;
using TL_Cloud.Models.BookShop;
using TL_Cloud.ViewModels.BookShop;

namespace TL_Cloud.RepositoryService.BookShop
{
    public class BaseGradeRepository : BaseRepository, IBaseGradeRepository
    {
        public BaseGradeRepository(AppBookShopContext myContext,
            IPropertyMappingContainer propertyMappingContainer) : base(myContext, propertyMappingContainer)
        {
        }

        public async Task<PaginatedList<Base_GradeInfo>> GetAllEntitiesAsync(GradeQueryParameters postParameters)
        {
            var query = _myContext.BaseGradeInfo.AsQueryable();

            if (!string.IsNullOrEmpty(postParameters.keyWord))
            {
                string keyword = postParameters.keyWord.ToLowerInvariant();
                query = query.Where(n => n.GradeName.ToLowerInvariant().Contains(keyword));
            }

            query = query.ApplySort(postParameters.OrderBy, _propertyMappingContainer.Resolve<BaseGradeResource, Base_GradeInfo>());

          
            List<Base_GradeInfo> data;
            int count = 0;
            if (postParameters.isPage)
            {
                count = await query.CountAsync();
                data = await query
                    .Skip(postParameters.PageIndex * postParameters.PageSize)
                    .Take(postParameters.PageSize)
                    .ToListAsync();
            }
            else
            {
                data = await query.ToListAsync();
            }
            return new PaginatedList<Base_GradeInfo>(postParameters.PageIndex, postParameters.PageSize, count, data);
        }

        public async Task<Base_GradeInfo> GetEntityByIdAsync(string id)
        {
            return await _myContext.BaseGradeInfo.FindAsync(id);
        } 

        public void Add(Base_GradeInfo entity)
        {
            _myContext.BaseGradeInfo.Add(entity);
        }

        public void Delete(Base_GradeInfo entity)
        {
            _myContext.BaseGradeInfo.Remove(entity);
        }

        public void Update(Base_GradeInfo entity)
        {
            _myContext.Entry(entity).State = EntityState.Modified;
        }

       
    }

 

}
